import barcode
from tkinter import CENTER
from barcode.writer import ImageWriter
from PIL import Image,ImageFont, ImageDraw



class barcodeGenerator:
    def __init__(self,code,filename,storageLocation):
        self.code=code
        self.filename=filename
        self.storageLocation=storageLocation
        EAN = barcode.get_barcode_class('code39')
        ean = EAN(code,writer=ImageWriter(),add_checksum=False)
        ean.save('barcode-bar',{'module_width':0.13,'module_height':4,'font_size':22,'text_distance':20,'text_distance':1,'write_text':False}) # 保存文件'erer'
#生成条形码
        txtimg=Image.new('RGBA',(451,50),'white')#生成图片及其参数
        draw=ImageDraw.Draw(txtimg)
        font=ImageFont.truetype('msyh.ttc',45)#文本字体
        draw.text((40,0),code,fill='black',font=font)#生成文本及其参数
        txtimg.save('barcode-text.png')
#生成文字
        barcodeImage=Image.new('RGBA',(451,130),'white')
        barImage=Image.open('barcode-bar.png')
        barcodeImage.paste(barImage,(0,42))
        textImage=Image.open('barcode-text.png')
        barcodeImage.paste(textImage,(0,0))
        barcodeImage.show()
        barcodeImage.save(storageLocation+filename+'.png')
#生成一个空白图片用来拼接两张图片

#使用之前请先安装python-barcode和pillow
logo=""" ____                          _       _____                           _             
|  _ \                        | |     / ____|                         | |            
| |_) | __ _ _ __ ___ ___   __| | ___| |  __  ___ _ __   ___ _ __ __ _| |_ ___  _ __ 
|  _ < / _` | '__/ __/ _ \ / _` |/ _ \ | |_ |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \| '__|
| |_) | (_| | | | (_| (_) | (_| |  __/ |__| |  __/ | | |  __/ | | (_| | || (_) | |   
|____/ \__,_|_|  \___\___/ \__,_|\___|\_____|\___|_| |_|\___|_|  \__,_|\__\___/|_|   
"""
print(logo)
storageLocation=input("请设置要存储的盘符:")
text=input("请输入序列号(用,进行分隔)：")
num=input("请输入需要生成的序列号数量：")
serialCode=[]
list=[]


for i in range(0,int(num)):
    serialCode=text.split(",")
    list.append(barcodeGenerator(serialCode[i],serialCode[i][-3:],storageLocation))

for i in list:
    print(i.code,i.filename,i.storageLocation)
#批量输入文本生成二维码